//
//  CedarButton.m
//  CedarFinancePH
//
//  Created by lshenrong on 2018/3/20.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "CedarButton.h"
#import "UIButton+ButtonStyle.h"
#import "UIView+Gradient.h"



@implementation CedarButton


+ (instancetype)buttonWithText:(NSString *)text {
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        [btn loginStyle];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

+ (instancetype)buttonWithBorderRegisterText:(NSString *)text {
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        [btn styleWithNormalColor:[UIColor whiteColor] highlightColor:kRegisterHighlightColor];
        [btn PFCGStyleTextColor:kGrayColor fontSize:16];
        [btn borderStyleWithColor:kRegisterColor cornerRadius:5];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

+ (instancetype)buttonWithText:(NSString *)text cornerRadius:(CGFloat)radius{
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        [btn loginStyle];
        [btn cornerRadiusStyle:radius];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}



+ (instancetype)buttonWithImageName:(NSString *)imageName{
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

+ (instancetype)buttonWithImage:(UIImage *)image{
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setImage:image forState:UIControlStateNormal];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}



+ (instancetype)buttonWithRightText:(NSString *)text leftImage:(NSString *)imageName{
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        btn.titleLabel.font = PingFangZhongChangGuiForFont(16);
        [btn setTitle:text forState:UIControlStateNormal];
        [btn setTitleColor:kWhiteColor forState:UIControlStateNormal];
        UIImage *image = [UIImage imageNamed:imageName];
        [btn setImage:image forState:UIControlStateNormal];
        [btn setSelected:YES];
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -btn.titleLabel.frame.size.width, 0, btn.titleLabel.frame.size.width);
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, btn.imageView.frame.size.width+5.5, 0, -btn.imageView.frame.size.width-5.5);
        [btn sizeToFit];
      //  btn.backgroundColor = [UIColor whiteColor];
    }
    return btn;
}

#pragma mark - 新的按钮方法

+ (instancetype)buttonWithTextNoStytle:(NSString *)text {
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        [btn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [btn setTitleColor:kLightBlackColor forState:UIControlStateHighlighted];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}


+ (instancetype)buttonWithTextNoStytle:(NSString *)text color:(UIColor *)color{
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        [btn setTitleColor:color forState:UIControlStateNormal];
        [btn setTitleColor:kGrayHighlightColor forState:UIControlStateHighlighted];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}


+ (instancetype)buttonWithRegisterText:(NSString *)text {
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        [btn cornerRadiusStyle:5];
        [btn PFCGStyleFontSize:20];
//        UIColor *startColor = [UIColor colorWithRGB:0x02C5BB];
//        UIColor *endColor = [UIColor colorWithRGB:0x0EA4A9];
//        [btn setGradientBackgroundWithColors:@[startColor,endColor] locations: @[@(0.0),@(1.0)] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
        //[btn addGradientLayer];
        [btn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0x4979E6"]] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageWithColor:kButonHighlightColor] forState:UIControlStateHighlighted];
        [btn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0x90AFF1"]] forState:UIControlStateDisabled];
        [btn whiteTextStyle];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

+ (instancetype)buttonWithText:(NSString *)text borderColor:(UIColor *)color{
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        [btn setTitleColor:color forState:UIControlStateNormal];
        UIColor *startColor = [UIColor colorWithRGB:0xf5f5f5];
        [btn setBackgroundImage:[UIImage imageWithColor:startColor] forState:UIControlStateHighlighted];
        [btn borderStyleWithColor:color cornerRadius:5];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

+ (instancetype)buttonWithText:(NSString *)text borderColor:(UIColor *)color textColor:(UIColor *)textColor{
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        [btn setTitleColor:textColor forState:UIControlStateNormal];
        [btn borderStyleWithColor:color cornerRadius:5];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}


+ (instancetype)buttonWithOrderText:(NSString *)text {
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        //[btn loginOrangeStyle];
        UIColor *startColor = kWhiteColor;// 247 148 59
        UIColor *endColor = kWhiteColor; //
        if ([text isEqualToString:@"确认接单"]) {
            startColor = [UIColor colorWithRGB:0x13C999];
            endColor = [UIColor colorWithRGB:0x2DD3AE];
        }
        if ([text isEqualToString:@"拒绝接单"] || [text isEqualToString:@"费用上报"] ) {
             [btn borderStyleWithColor:kSeparateLineColor cornerRadius:15];
            [btn fontColorStyle:kBlackColor];
        }
        
        if ([text containsString:@"辆"]) {
             [btn borderStyleWithColor:[UIColor colorWithRGB:0x556FB5] cornerRadius:3];
            [btn fontColorStyle:[UIColor colorWithRGB:0x556FB5]];
        }
        
        if ([text containsString:@"天"]) {
             [btn borderStyleWithColor:kOrangeColor cornerRadius:3];
            [btn fontColorStyle:kOrangeColor];
        }
        if ([text isEqualToString:@"发布任务"]  ) {
             [btn borderStyleWithColor:kOrangeColor cornerRadius:15];
            [btn fontColorStyle:kOrangeColor];
        }
        [btn setGradientBackgroundWithColors:@[startColor,endColor] locations: @[@(0.0),@(1.0)] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
//        [btn setBackgroundImage:[UIImage imageWithColor:startColor] forState:UIControlStateHighlighted];
        if ([text isEqualToString:@"拒绝接单"] || [text isEqualToString:@"费用上报"] ) {
            [btn setBackgroundImage:[UIImage imageWithColor:kWhiteColor] forState:UIControlStateDisabled];
        }else{
             [btn setBackgroundImage:[UIImage imageWithColor:kWhiteColor] forState:UIControlStateDisabled];
        }
       
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

+ (instancetype)buttonWithBuyTextNoGradientLayer:(NSString *)text {
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        [btn loginOrangeStyle];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

//专为为产品变迁
+ (instancetype)buttonWithProductTagText:(NSString *)text  {
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        [btn borderStyleWithColor:kBlueColor cornerRadius:8];
        [btn PFCGStyleTextColor:kBlueColor fontSize:10];
        btn.backgroundColor = [UIColor colorWithRGB:0xe3ebfb];
        btn.contentEdgeInsets = UIEdgeInsetsMake(2, 3, 2, 3);
    }
    return btn;
}

+ (instancetype)buttonWithLeftText:(NSString *)text
                        rightImage:(NSString *)imageName
                   backgroundImage:(NSString *)bgImage{
    
    return [self buttonWithLeftText:text rightImage:imageName backgroundImage:bgImage topOffset:0];
    /*
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        if (SCREEN_WIDTH == 320) {
            btn.titleLabel.font = PingFangZhongChangGuiForFont(11);
        }else{
            btn.titleLabel.font = PingFangZhongChangGuiForFont(13);
        }
        [btn setTitle:text forState:UIControlStateNormal];
        [btn setTitleColor:kWhiteColor forState:UIControlStateNormal];
        UIImage *image = [UIImage imageNamed:imageName];
        [btn setImage:image forState:UIControlStateNormal];
        [btn setSelected:YES];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [btn sizeToFit];
        if (SCREEN_WIDTH == 320) {
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width-4, 0, btn.imageView.frame.size.width);
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width+4, 0, -btn.titleLabel.frame.size.width);
        }else{
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width-8, 0, btn.imageView.frame.size.width);
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width+8, 0, -btn.titleLabel.frame.size.width);
        }
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        if(bgImage){
            [btn setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateNormal];
        }
    }
    return btn;*/
}

+ (instancetype)buttonWithLeftText:(NSString *)text
                        rightImage:(NSString *)imageName
                   backgroundImage:(NSString *)bgImage
                         topOffset:(CGFloat)offsetX
    {
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        if (SCREEN_WIDTH == 320) {
            btn.titleLabel.font = PingFangZhongChangGuiForFont(11);
        }else{
            btn.titleLabel.font = PingFangZhongChangGuiForFont(13);
        }
        [btn setTitle:text forState:UIControlStateNormal];
        [btn setTitleColor:kWhiteColor forState:UIControlStateNormal];
        UIImage *image = [UIImage imageNamed:imageName];
        [btn setImage:image forState:UIControlStateNormal];
        [btn setSelected:YES];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [btn sizeToFit];
        if (SCREEN_WIDTH == 320) {
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width-4, 0, btn.imageView.frame.size.width);
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width+4, 0, -btn.titleLabel.frame.size.width);
        }else{
            btn.titleEdgeInsets = UIEdgeInsetsMake(offsetX, -btn.imageView.frame.size.width-8, 0, btn.imageView.frame.size.width);
            btn.imageEdgeInsets = UIEdgeInsetsMake(offsetX, btn.titleLabel.frame.size.width+8, 0, -btn.titleLabel.frame.size.width);
        }
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        if(bgImage){
            [btn setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateNormal];
        }
    }
    return btn;
}

+ (instancetype)buttonWithText:(NSString *)text
               backgroundImage:(NSString *)bgImage{
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        if (SCREEN_WIDTH == 320) {
            btn.titleLabel.font = PingFangZhongChangGuiForFont(11);
        }else{
            btn.titleLabel.font = PingFangZhongChangGuiForFont(13);
        }
        [btn setTitle:text forState:UIControlStateNormal];
        [btn loginStyle];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
        if (SCREEN_WIDTH == 320) {
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, btn.imageView.frame.size.width);
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width+4, 0, -btn.titleLabel.frame.size.width);
        }else{
            btn.titleEdgeInsets = UIEdgeInsetsMake(-3, 0, 0, btn.imageView.frame.size.width);
            btn.imageEdgeInsets = UIEdgeInsetsMake(-3, btn.titleLabel.frame.size.width+8, 0, -btn.titleLabel.frame.size.width);
        }
        if(bgImage){
            [btn setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateNormal];
        }
    }
    return btn;
}



#pragma mark - Gradient button



+ (instancetype)buttonWithTextForGradientForCommomn:(NSString *)text radius:(BOOL)hasRadius{
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [btn PFCGStyleTextColor:kWhiteColor fontSize:16];
        if(hasRadius){
            [btn cornerRadiusStyle:20];
        }
        
        UIColor *startColor = [UIColor colorWithRGB:0x13C999];
        UIColor *endColor = [UIColor colorWithRGB:0x2DD3C1];
        [btn setGradientStartColor:startColor endColor:endColor];
    }
    return btn;
}



+ (instancetype)buttonWithTextForGradient:(NSString *)text {
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];

       /* UIColor *startColor = [UIColor colorWithRGB:0xF7943B];// 247 148 59
        UIColor *endColor = [UIColor colorWithRGB:0xF77A09]; //
        [btn setGradientBackgroundWithColors:@[startColor,endColor] locations: @[@(0.0),@(1.0)] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
        [btn setBackgroundImage:[UIImage imageWithColor:startColor] forState:UIControlStateHighlighted];
        [btn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0xd3d3d3"]] forState:UIControlStateDisabled];*/
    }

    return btn;
}

- (void)setGradientStartColor:(UIColor *)startColor endColor:(UIColor *)endColor{
    [self setGradientBackgroundWithColors:@[startColor,endColor] locations: @[@(0.0),@(1.0)] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
    [self setBackgroundImage:[UIImage imageWithColor:startColor] forState:UIControlStateHighlighted];
    //[self setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"0xd3d3d3"]] forState:UIControlStateDisabled];
}



+ (instancetype)buttonWithText:(NSString *)text borderColor:(UIColor *)color selectedColor:(UIColor *)selectedColor {
    CedarButton *btn = [self buttonWithType:UIButtonTypeCustom];
    if (btn) {
        [btn setTitle:text forState:UIControlStateNormal];
        [btn setTitleColor:kGrayText2Color forState:UIControlStateNormal];
        [btn setTitleColor:kWhiteColor forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageWithColor:selectedColor] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        CGFloat width = btn.width;
        [btn borderStyleWithColor:color cornerRadius:width / 2];
        [btn addTarget:btn action:@selector(clickButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

#pragma mark - 带有渐变色

- (void)addGradientLayer{
    UIColor *startColor = [UIColor colorWithRGB:0x02C5BB];
    UIColor *endColor = [UIColor colorWithRGB:0x0EA4A9];
    [self setGradientBackgroundWithColors:@[startColor,endColor] locations: @[@(0.0),@(1.0)] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
}

- (void)clickButtonAction{
    if (self.clickEvent) {
        self.clickEvent();
    }
}




LogDeallocFunc


@end
